﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using LitJson;
using System.IO;


//武器类工厂模式，解析json文件， 并获取单个武器的属性
public class WeaponFactory : ReflectSingleton<WeaponFactory>
{
    List<Weapon> weapons = new List<Weapon>();

    //私有构造函数（只能调用类内部的对象）
    private WeaponFactory()
    {
        ReadWeaponDate();
    }
    //解析json文件，赋值给weapon对象
    public void ReadWeaponDate()
    {
        //获取json文件路径
        string path = Application.dataPath + "/Resources/json.json";
        string jsonStr = File.ReadAllText(path);
        //解析
        JsonData jd= JsonMapper.ToObject(jsonStr);
        JsonData jdArr = jd["content"];
        for (int i = 0; i < jdArr.Count; i++)
        {
            Weapon w = new Weapon();
            w.id = int.Parse(jdArr[i]["ID"].ToString());
            w.name = jdArr[i]["名字"].ToString();
            w.quality = int.Parse(jdArr[i]["品质"].ToString());
            w.atk = int.Parse(jdArr[i]["每秒伤害"].ToString());
            weapons.Add(w);
        }
    }

    //通过id获取武器属性
    public Weapon GetWeaponByID(int id)
    {
        Weapon weapon = null;

        foreach (Weapon w in weapons    )
        {
            if (w.id==id)
            {
                weapon = w;
            }
        }return weapon;
    }

    //通过name获取武器属性
    public Weapon GetWeaponByName(string name)
    {
        Weapon weapon = null;
        foreach ( Weapon w in weapons)
        {
            if (w.name==name)
            {
                weapon = w;

            }
        }
        return weapon;
    }

}
